home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / OOP.SWG / 0047_Scrollbar Object.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  5KB  |  224 lines

  1. {
  2. I recently made a SCROLLBAR object. it's pretty good and draws it's strings
  3. directly to videomemory so no flashing etc.
  4. }
  5.  
  6. UNIT scroller;
  7.  
  8. { scrolbar.init(left, top, maxright, maxbottom, startvalue)
  9.  if maxright and maxbottom are high enough
  10.  the windowsize will be adjusted automatically to
  11.  the longest string in the data-array.
  12.  Mind that the scroller stops counting as soon as
  13.  an empty string is encountered, and everything below
  14.  it will not be displayed.
  15.  in order to save memory your application may write
  16.  directly to scrollbar.data.
  17.  
  18.  scrollbar.return contains the return value, i.e. the index
  19.  of the chosen element in the data-array.
  20.  
  21.   scroll.bar.incr/decr take an integer and increases/decreases
  22.  scrollbar.return
  23.  
  24.   scrollbar.update should be called after each scrollbar.incr/decr
  25.  to display the changes.
  26.  
  27.   scrollbar.borderhit is a boolean that becomes true
  28.  when the user attempts to go past the first/last item in
  29.  the list. This should be used BEFORE scrollbar.update, as
  30.  that procedure sets both booleans to False.
  31.   scrollbar.stop closes the window and restores the screen.
  32.  
  33.   willem van de vis
  34.   s0730076@let.rug.nl
  35.  
  36. }
  37.  
  38. Interface
  39.  
  40. const max = 200;
  41.       maxwidth = 20;
  42. type
  43.    scrolldata = array[1..max] of string[maxwidth];
  44.  
  45. type Scrollbar =
  46.   object
  47.    private {erase private and public for tp6.0}
  48.     top,
  49.     bottom,
  50.     left,
  51.     right,
  52.     total,
  53.     width,
  54.     len,
  55.     y,
  56.     b  : integer;
  57.     public
  58.     data : scrolldata;
  59.     return : integer;
  60.     borderhit: boolean;
  61.                 constructor init(ileft,itop, iright,ibottom,iq: integer);
  62.  
  63.     destructor  stop;
  64.     procedure   update;
  65.     procedure   incr(i : integer);
  66.     procedure   decr(d : integer);
  67.    end;
  68.  
  69. Implementation
  70. uses win, crt;
  71.  
  72. constructor scrollbar.init;
  73. var q : integer;
  74. begin
  75.  
  76.  if ibottom > 24 then ibottom := 24;
  77.  if itop < 2 then itop := 1;
  78.  if ileft < 2 then ileft := 1;
  79.  if iright > 78 then iright := 78;
  80.  
  81.     return := 1;
  82.     b      := 1;
  83.     y      := 1;
  84.  bottom := ibottom;
  85.  top    := itop;
  86.  len    := (bottom - top)+1;
  87.  left   := ileft-1;
  88.  right  := iright;
  89.  borderhit := false;
  90.  total := 1;
  91.  width := 1;
  92.  
  93.  while (total <= max) and (data[total] <> '') do
  94.  begin
  95.   if length(data[total]) > width then width := length(data[total]);
  96.   data[total] := data[total] +
  97.  '                                                                   ';
  98.   inc(total);
  99.  end;
  100.  dec(total);
  101.  if total = 0 then total := 1;
  102.     if width < right - ileft then right := left + width
  103.       else width := right-ileft;
  104.       dec(right);
  105.       if total < bottom - itop then bottom := itop + total-1;
  106.       open_win(ileft, itop, right, bottom, lightgray, blue);
  107.     if iq > 1 then
  108.     scrollbar.incr(iq-1);
  109. end;
  110.  
  111. destructor scrollbar.stop;
  112. begin
  113.  close_win;
  114. end;
  115.  
  116. procedure scrollbar.decr;
  117. begin
  118.  if return - d < 1 then
  119.   d := return-1;
  120.   dec(return,d);
  121.   if (y - d < 1) then
  122.   begin
  123.    b := return;
  124.    y := 1;
  125.   end
  126.   else
  127.     dec(y,d);
  128.  
  129.   if d = 0 then borderhit := true;
  130. end;
  131.  
  132. procedure scrollbar.incr;
  133. begin
  134.  if return + i > total then
  135.  i := total - return;
  136.  
  137.   inc(return,i);
  138.   if (y + i > len) then
  139.   begin
  140.   b := 1 + return - len;
  141.    y := len;
  142.   end
  143.   else
  144.     inc(y,i);
  145.  if i = 0 then borderhit := true;
  146. end;
  147.  
  148.  
  149. procedure scrollbar.update;
  150. var i : integer;
  151. begin
  152.  borderhit := false;
  153.  for i := b  to b + len-1 do
  154.   if i <= total then
  155.  
  156.    if i - b + 1 = y then
  157.      writeline(data[i],left, top+i-b, width, white,black)
  158.     else
  159.      writeline(data[i],left, top+i-b, width, lightgray,blue);
  160.  
  161. end;
  162.  
  163. end.
  164.  
  165. Mind that no pointers are used so there IS a max to the number of lines
  166. displayed. It may be neccesary to adjust width en right etc.
  167.  
  168. an example of an application:
  169.  
  170. uses scroller,ill,crt,win;
  171.  
  172. var scrol  : scrollbar;
  173.  r   : char;
  174.  chosen : string;
  175.  F  : TEXT;
  176.  q  : integer;
  177.  
  178. begin
  179.  
  180. assign(f, paramstr(1));
  181. reset(f);
  182. q := 1;
  183.  
  184. while (not eof(f)) and (q < 500) do
  185. begin
  186.  readln(f,scrol.data[q]);
  187.  if scrol.data[q] <> '' then
  188.  inc(q);
  189. end;
  190. close(f);
  191.  
  192. ini_win;
  193.  
  194. scrol.init(2,2,80,18,1);
  195.  
  196. r := '!';
  197.  
  198.  
  199.  
  200. while (r <> chr(13)) and (r <> chr(27)) do
  201. begin
  202.  
  203.  if scrol.borderhit then brom;
  204.  scrol.update;
  205.  r := readkey;
  206.  case r of
  207.   chr(72) : scrol.decr(1);
  208.   chr(80) : scrol.incr(1);
  209.   chr(73) : scrol.decr(10);
  210.   chr(81) : scrol.incr(10);
  211.  
  212.  end;
  213.  
  214.  
  215. end;
  216.  
  217. chosen := scrol.data[scrol.return];
  218. scrol.stop;
  219. if r <> chr(27) then
  220. begin
  221.  writeln(chosen);
  222. end;
  223. end.
  224.